<?php
/**
 *+------------------
 * Lflow
 *+------------------
 * Copyright (c) 2023~2030 gitee.com/liu_guan_qing All rights reserved.本版权不可删除，侵权必究
 *+------------------
 * Author: Mr.April(405784684@qq.com)
 *+------------------
 */
declare (strict_types=1);

namespace lflow\access;

/**
 * 实现有关分页参数及查询结果封装
 *
 * @author Mr.April
 * @since  1.0
 */
class Page
{
    /**
     * 默认页size
     */
    const PAGE_SIZE = 15;

    /**
     * 当前页
     *
     * @var int
     */
    private int $pageNum = 1;

    /**
     * 每页数量
     *
     * @var int
     */
    private int $pageSize = -1;

    /**
     * 最后页码
     *
     * @var int
     */
    private int $totalPage = -1;

    /**
     * 总记录数
     *
     * @var int
     */
    private int $recordCount = 0;

    /**
     * 结果集
     *
     * @var array
     */
    private array $rows = [];

    /**
     * 初始化
     */
    public function __construct()
    {
        $this->pageNum = self::PAGE_SIZE;
    }

    /**
     * Page
     *
     * @param int $pageSize
     */
    public function Page(int $pageSize): void
    {
        $this->pageSize = $pageSize;
    }

    /**
     * 获得当前页的页号,默认为1
     *
     * @return int
     */
    public function getPageNo(): int
    {
        return $this->pageNum;
    }

    /**
     * 设置当前页的页号,小于1时自动设置为1.
     *
     * @param int $pageIndex
     */
    public function setPageIndex(int $pageIndex): void
    {
        $this->pageNum = $pageIndex;
        if ($pageIndex < 1) {
            $this->pageNum = 1;
        }
    }

    /**
     * 返回Page对象自身的setPageNo函数,可用于连续设置。
     *
     * @param int $thePageNo
     *
     * @return $this
     */
    public function pageIndex(int $thePageNo): Page
    {
        $this->setpageIndex($thePageNo);
        return $this;
    }

    /**
     * 获得每页记录数.
     *
     * @return int
     */
    public function getPageSize(): int
    {
        return $this->pageSize;
    }

    /**
     * 设置每页的记录数
     *
     * @param int $pageSize
     */
    public function setPageSize(int $pageSize): void
    {
        $this->pageSize = $pageSize;
    }

    /**
     * 返回Page对象自身的setPageSize函数,用于连续设置。
     *
     * @param int $thePageSize
     *
     * @return $this
     */
    public function pageSize(int $thePageSize): Page
    {
        $this->setPageSize($thePageSize);
        return $this;
    }

    /**
     * 获得页内的记录列表
     *
     * @return array
     */
    public function getResult(): array
    {
        return $this->rows ?? [];
    }

    /**
     * 设置业内的记录列表
     *
     * @param array $result
     */
    public function setResult(array $result = []): void
    {
        $this->rows = $result;
    }

    /**
     * 默认值0
     *
     * @return int
     */
    public function getTotalCount(): int
    {
        return max($this->recordCount, 0);
    }

    /**
     * 设置总记录数
     *
     * @param int $totalCount
     */
    public function setTotalCount(int $totalCount): void
    {
        $this->recordCount = $totalCount;
        if ($this->getTotalCount() > 0) {
            $this->totalPage = (int)ceil($this->getTotalCount() / $this->getPageSize());
        }
    }

    /**
     * 根据pageSize与totalCount计算总页数, 默认值为-1
     *
     * @return int
     */
    public function getTotalPages(): int
    {
        if ($this->recordCount < 0) {
            return 0;
        }
        return $this->recordCount / $this->pageSize;
    }

    /**
     * 是否还有下一页
     *
     * @return bool
     */
    public function isHasNext(): bool
    {
        return (($this->pageNum + 1) <= $this->getTotalPages());
    }

    /**
     * 取得下页的页号, 序号从1开始当前页为尾页时仍返回尾页序号
     *
     * @return int
     */
    public function getNextPage(): int
    {
        if ($this->isHasNext()) {
            return $this->pageNum + 1;
        } else {
            return $this->pageNum;
        }
    }

    /**
     * 是否还有上一页
     *
     * @return bool
     */
    public function isHasPre(): bool
    {
        return (($this->pageNum - 1) >= 1);
    }

    /**
     * 取得上页的页号, 序号从1开始当前页为首页时返回首页序号
     *
     * @return int
     */
    public function getPrePage(): int
    {
        if ($this->isHasPre()) {
            return $this->pageNum - 1;
        } else {
            return $this->pageNum;
        }
    }

    /**
     * 输出字符串
     *
     * @return string
     */
    public function toString(): string
    {
        return json_encode(get_object_vars($this));
    }

    /**
     * 输出array
     *
     * @return array
     */
    public function toArray(): array
    {
        return get_object_vars($this);
    }
}